package com.xzx.study.provider;

import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.xzx.study.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SelectContactActivity extends ListActivity {
    private static final String TAG = "TAG-" + SelectContactActivity.class.getSimpleName();

    private BaseAdapter baseAdapter;
    private ListView listView;
    private List<Map<String, String>> data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_contact);

        baseAdapter = new PhoneAdapter();
        listView = getListView();
        initData();
        listView.setAdapter(baseAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra("number", data.get(position).get("number"));
                Log.i(TAG, data.get(position).get("number"));
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    private void initData() {
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{
                                              ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                                              ContactsContract.CommonDataKinds.Phone.NUMBER},
                                      null, null, null);
        while (cursor.moveToNext()) {
            String name = cursor.getString(0);
            String number = cursor.getString(1);
            Map<String, String> map = new HashMap<>();
            map.put("name", name);
            map.put("number", number);
            data.add(map);
        }
        cursor.close();
    }


    class PhoneAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(SelectContactActivity.this, R.layout.item_select_phone, null);
            }
            TextView name = convertView.findViewById(R.id.select_phone_name);
            TextView number = convertView.findViewById(R.id.select_phone_number);
            Map<String, String> map = data.get(position);
            name.setText(map.get("name"));
            number.setText(map.get("number"));
            return convertView;
        }
    }
}
